package luckFoster

import (
	"github.com/gin-gonic/gin"
	"my-systems/pet-system/internal/service"
	"my-systems/pet-system/pkg/e"
	"my-systems/pet-system/pkg/tools"
	"net/http"
)

func AddFoster(ctx *gin.Context) {
	fosterTmp := &service.Foster{}
	err := ctx.ShouldBind(fosterTmp)
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(e.StructBindingFailed, gin.H{
			"error": err.Error(),
			"msg":   e.GetMsgInfo(e.StructBindingFailed),
		})
		return
	}

	// 获取token
	tokenString := ctx.GetHeader("authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}

	res := fosterTmp.AddFoster(claims.ID)
	ctx.JSON(http.StatusOK, res)
}

func UpdateFoster(ctx *gin.Context) {
	fosterTmp := &service.Foster{}
	err := ctx.ShouldBind(fosterTmp)
	if err != nil {
		tools.Logger.Error(err.Error())
		ctx.JSON(e.StructBindingFailed, gin.H{
			"error": err.Error(),
			"msg":   e.GetMsgInfo(e.StructBindingFailed),
		})
		return
	}
	// 获取token
	tokenString := ctx.GetHeader("authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}

	res := fosterTmp.UpdateFoster(claims.ID)
	ctx.JSON(http.StatusOK, res)
}

func DeleteFosterByID(ctx *gin.Context) {
	fosterTmp := &service.Foster{}
	fosterID := ctx.Param("id")
	// 获取token
	tokenString := ctx.GetHeader("authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}

	res := fosterTmp.DeleteFosterByID(claims.ID, fosterID)
	ctx.JSON(http.StatusOK, res)
}

func GetFosters(ctx *gin.Context) {
	fosterTmp := &service.Foster{}
	// 获取token
	tokenString := ctx.GetHeader("authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}
	res := fosterTmp.GetFosters(claims.ID)
	ctx.JSON(http.StatusOK, res)
}

func GetFosterByID(ctx *gin.Context) {
	fosterTmp := &service.Foster{}
	// 获取token
	tokenString := ctx.GetHeader("authorization")
	if tokenString == "" {
		ctx.JSON(http.StatusBadRequest, gin.H{
			"code": e.SuspectedTokenDoesNotExist,
			"msg":  e.GetMsgInfo(e.SuspectedTokenDoesNotExist),
		})
		return
	}
	// 解析token
	claims, err := tools.ParseToken(tokenString)
	if err != nil {
		ctx.JSON(http.StatusInternalServerError, gin.H{
			"error": "解析 Token 失败",
		})
		return
	}
	fosterID := ctx.Param("id")
	res := fosterTmp.GetFosterByID(claims.ID, fosterID)
	ctx.JSON(http.StatusOK, res)
}
